package com.cms.admin.core.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cms.admin.util.Constants;
import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter;

public class MyStrutsFilter extends StrutsPrepareAndExecuteFilter {

	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response=(HttpServletResponse) res;

		String url = request.getRequestURI();
		String contextPath=request.getContextPath()+"/";
		String loginUrl=contextPath+"privilege/login.action";
		String loginjsp=contextPath+"AdminLTE/pages/examples/login.jsp";
		if (contextPath.equals(url)||loginUrl.equals(url)||loginjsp.equals(url)) {
			super.doFilter(req, res, chain);
		} else {
			HttpSession session=request.getSession();
			if (null!=session.getAttribute(Constants.USER_OBJECT))
			{
				super.doFilter(req, res, chain);
			}else
			{
				response.sendRedirect(contextPath);return;
			}
		}
	}

}
